package org.lql.algo.codecrush.week009.question.type.IIII;

import org.lql.algo.common.TreeNode;

/**
 * @author: liangqinglong
 * @date: 2025-08-20 17:04
 * @description: LCR 174. 寻找二叉搜索树中的目标节点 <a href="https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/description/">...</a>
 **/
public class FindTargetNode {

	private int count;
	private int ans;

	public int findTargetNode(TreeNode root, int cnt) {
		count = cnt;
		find(root);
		return ans;
	}

	public void find(TreeNode root) {
		if (root == null) {
			return;
		}
		find(root.right);
		if (count == 0) {
			return;
		}
		count--;
		if (count == 0) {
			ans = root.val;
		}
		find(root.left);
	}
}
